import com.google.gson.Gson;

/**
 * Created by wangxingsheng on 2/22/22.
 * desc:排序算法实现
 */
public class SoftTest {
    public static void main(String[] args) {
        int[] src = new int[]{3, 6, 2, 8, 1, 9, 3, 12, 0};
        printArray(soft1(src));
        printArray(soft2(src));
    }

    static void printArray(int[] res) {
        System.out.println(new Gson().toJson(res));
    }


    /**
     * 冒泡排序
     *
     * @param src
     * @return
     */
    static int[] soft1(int[] src) {
        int[] res = src.clone();
        int n = res.length;
        for (int i = 0; i < n - 1; i++) {
            for (int j = i + 1; j < n; j++) {
                if (res[i] > res[j]) {
                    int temp = res[i];
                    res[i] = res[j];
                    res[j] = temp;
                }
            }
        }
        return res;
    }

    static int[] soft2(int[] src) {
        int[] res = src.clone();
        int n = res.length;
        int minIndex, temp;
        for (int i = 0; i < n - 1; i++) {
            minIndex = i;
            for (int j = i + 1; j < n; j++) {
                if(res[j] < res[minIndex]){
                    minIndex = j;
                }
            }
            temp = res[i];
            res[i] = res[minIndex];
            res[minIndex] = temp;
        }
        return res;
    }

}
